package cn.dnui_lf_705.dnui_16110100705.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.FoodInfo;
import cn.dnui_lf_705.dnui_16110100705.controller.FoodInfoActivity;
import cn.dnui_lf_705.dnui_16110100705.model.Constant;


public class FoodListAdapter extends  RecyclerView.Adapter<FoodListAdapter.ViewHolder> implements View.OnClickListener{
    private List<FoodInfo> mDatalist;
    private Context Context;
    private LayoutInflater mLayoutInflater;
    private String phonenum;
    public FoodListAdapter(Context context, List data, int food_item, String phonenum) {
        this.Context = context;
        this.mDatalist = data;
        mLayoutInflater = LayoutInflater.from(Context);
        this.phonenum=phonenum;
       // notifyDataSetChanged();             //刷新页面
    }
    public FoodListAdapter(Context context, List data) {
        this.Context = context;
        this.mDatalist = data;
        mLayoutInflater = LayoutInflater.from(Context);
        // notifyDataSetChanged();             //刷新页面
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public FoodListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v =mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.food_item,viewGroup,false);
        v.setOnClickListener(this);
        return new FoodListAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull FoodListAdapter.ViewHolder viewHolder1, int i) {
        final FoodInfo entity = mDatalist.get(i);
        if(entity == null)
            return;
        final ViewHolder viewHolder = viewHolder1;
        viewHolder.food_name.setText(entity.getFoodname());
        viewHolder.price.setText(entity.getPrice()+"元");
        viewHolder.food_pinglun.setText(entity.getIntro());
        if(!entity.getPic().equals("")){
            Picasso.with(Context).load(Constant.BASE_URL+entity.getPic()).into(viewHolder.image);
        }
        viewHolder.food_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到菜单详情
                String foodid = String.valueOf(entity.getFood_id());
                Intent intent = new Intent(Context,FoodInfoActivity.class);
                intent.putExtra("id",foodid);
                intent.putExtra("phonenum",phonenum);
                Context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
       return mDatalist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView food_name,food_pinglun,price;
        ImageView image;
        LinearLayout food_container;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            food_name = itemView.findViewById(R.id.food_name);
            food_pinglun = itemView.findViewById(R.id.res_pinglun);
            price = itemView.findViewById(R.id.price);
            image = itemView.findViewById(R.id.food_image);
            food_container = itemView.findViewById(R.id.ll_food_list);
        }
    }
}
